<?php //echo "<pre>"; print_r($selectedUser);die();?>
<?php $this -> load -> view('header'); ?>
<!-- BEGIN CONTAINER -->
<div class="page-container row-fluid">
	<?php //$this->load->view('sidebar');?>
	<!-- BEGIN PAGE -->
	<div class="page-content">
		<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->
		<div id="portlet-config" class="modal hide">
			<div class="modal-header">
				<button data-dismiss="modal" class="close" type="button"></button>
				<h3>portlet Settings</h3>
			</div>
			<div class="modal-body">
				<p>
					Here will be a configuration form
				</p>
			</div>
		</div>
		<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
		<!-- BEGIN PAGE CONTAINER-->
		<div class="container-fluid">
			<!-- BEGIN PAGE HEADER-->
			<div class="row-fluid">
				<div class="span12">
					<!-- BEGIN STYLE CUSTOMIZER -->
					<!--
					<div class="color-panel hidden-phone">
						<div class="color-mode-icons icon-color"></div>
						<div class="color-mode-icons icon-color-close"></div>
						<div class="color-mode">
							<p>
								THEME COLOR
							</p>
							<ul class="inline">
								<li class="color-black current color-default" data-style="default"></li>
								<li class="color-blue" data-style="blue"></li>
								<li class="color-brown" data-style="brown"></li>
								<li class="color-purple" data-style="purple"></li>
								<li class="color-white color-light" data-style="light"></li>
							</ul>
							<label class="hidden-phone">
								<input type="checkbox" class="header" checked value="" />
								<span class="color-mode-label">Fixed Header</span> </label>
						</div>
					</div> -->
					<!-- END BEGIN STYLE CUSTOMIZER -->

					<!-- BEGIN PAGE TITLE & BREADCRUMB-->
					<?php $this -> load -> view('breadcrumb'); ?>
					<!-- END PAGE TITLE & BREADCRUMB-->
				</div>
			</div>
			<!-- END PAGE HEADER-->
			<div class="row-fluid">
				<div class="span12">
					<!-- BEGIN EXAMPLE TABLE PORTLET-->
					<div class="portlet box blue">
						<div class="portlet-title">
							<h4><i class="icon-edit">Tambah Pengguna</i></h4>
							<!--<div class="tools" >
								<a href="javascript:;" class="reload"></a>
							</div>-->
						</div>
						<div class="portlet-body">
							<form id="userForm" action="<?php echo base_url()?>administration/saveUser" method="post" class="form-horizontal">
					           <div class="control-group">
					              <label class="control-label">Nama Pengguna</label>
					              <div class="controls">
					                  <input type="text" class="span3" name="User.userId" id="pUserId" value="<?php if ($selectedUser!=NULL) echo $selectedUser->getUserId(); else echo '';?>" <?php if ($selectedUser!=NULL) echo "readonly"; else echo '';?>/>                  
					              </div>
					           </div>
							   <div class="control-group">
					              <label class="control-label">Nama</label>
					              <div class="controls">
					                  <input type="text" class="span3" name="User.name" id="pName" value="<?php if ($selectedUser!=NULL) echo $selectedUser->getName(); else echo '';?>"/>                  
					              </div>
					           </div>
							   <div class="control-group">
					              <label class="control-label">Email Pengguna</label>
					              <div class="controls">
					                  <input type="text" class="span3" name="User.mail" id="pMail" value="<?php if ($selectedUser!=NULL) echo $selectedUser->getMail(); else echo '';?>"/>                  
					              </div>
					           </div>
							   <div class="control-group">
					              <label class="control-label">Status Pengguna</label>
					              <div class="controls">
					                  <input type="radio" class="span3" name="User.status" id="pStatusActive" value="1" <?php if ($selectedUser!=NULL && $selectedUser->getStatus() == 1) echo 'checked'; else echo '';?>/> Active                
					                  <input type="radio" class="span3" name="User.status" id="pStatusInactive" value="0" <?php if ($selectedUser!=NULL && $selectedUser->getStatus() == 0) echo 'checked'; else echo '';?>/> Inactive                 
					              </div>
					           </div>
					           <div class="control-group" >

					              <label class="control-label">Password</label>
					              <div class="controls">
					                  <input type="text" class="span3" name="User.password" id="pPassword" value="<?php if ($selectedUser!=NULL) echo $selectedUser->getPassword(); else echo '';?>" />                  
					              </div>
					           </div>
					           <div class="control-group">
					                <label class="control-label">Jabatan</label>
					                   <div class="controls">
					                     <select class="span3 chosen" data-placeholder="--Pilih Jabatan--" name="User.Jabatan" tabindex="1" id="pJabatan">
					                        <option value="-1">--Pilih Jabatan--</option>
					                        <?php
					                        	foreach ($jabatans as $jabatan){
					                        		if ($selectedUser != NULL){
					                        			if ($selectedUser->getJabatan() == $jabatan){
					                        				echo '<option value="'.$selectedUser->getJabatan()->getId().'" selected>'.$selectedUser->getJabatan()->getNama().'</option>';
					                        			} else {
					                        				echo '<option value="'.$jabatan->getId().'" >'.$jabatan->getNama().'</option>';
					                        			}
					                        		} else {
					                        			echo '<option value="'.$jabatan->getId().'" >'.$jabatan->getNama().'</option>';
					                        		}
					                        	}
					                        ?>					                        
					                 	 </select>
					              </div>
					           </div>
					           <div class="control-group">
	                              <label class="control-label">Profil Pengguna</label>
	                              <div class="controls">
	                                 <select data-placeholder="Pilih Profil Pengguna" name="User.Profil[]" class="chosen span6" multiple="multiple" tabindex="6" id="pProfil">
	                                <!--   <option value="6">ENTRY1</option>
	                                   <option value="7" selected="selected" >VERIFIER1</option>
	                                   <option value="8" >VERIFIER1_2</option>
	                                   <option value="9" >APPROVAL1</option>
	                                   <option value="10" selected="selected">ENTRY2</option>
	                                   <option value="11" >VERIFIER2</option> --> 
	                                 <!--  <option selected>Chicago Bears</option>
                                       <option>Detroit Lions</option>
                                       <option>Green Bay Packers</option>
                                       <option>Minnesota Vikings</option>   -->                           
					                        <?php
					                       	if ($selectedUser != NULL){
					                        		$criteria = array('userId' => $selectedUser->getUserId());
													$userProfiles = $this -> em -> getRepository('\models\entities\UserProfile')->findBy($criteria);
													foreach ($userProfiles as $row){
														$p[] = $row->getProfile()->getId();
													}	
													//echo "<pre>";print_r($p);
													foreach ($profiles as $profile){
														if (in_array($profile->getId(), $p)){
															echo '<option value="'.$profile->getId().'" selected="selected">'.$profile->getNamaProfile().'</option>';
														} else {
															echo '<option value="'.$profile->getId().'" >'.$profile->getNamaProfile().'</option>';	
														}														
													}
												} else {
													foreach ($profiles as $profile){
														echo '<option value="'.$profile->getId().'" >'.$profile->getNamaProfile().'</option>';
													}		
												} 
					                        	
					                        	
					                        ?>                              
	                                 </select>
	                              </div>
	                           </div>
	                           <div class="control-group">
	                              <label class="control-label">Nomor Telpon</label>
	                              <div class="controls">
	                                 <input type="text" class="m-wrap span6 onlynumber" placeholder="62" name="msisdn" value="<?php echo ($selectedUser != NULL)? $selectedUser->getMsisdn():"";?>" />
								  </div>
	                           </div>
					           <div class="form-actions">
									<button type="submit" class="btn blue" id="saveBtn">
										<i class="icon-ok"></i> Simpan
									</button>
									<button type="button" class="btn" id="cancelBtn">
											Batal
									</button>
								</div>
					       </form>
						</div>
					</div>
					<!-- END EXAMPLE TABLE PORTLET-->
				</div>
			</div>

			
			<!-- BEGIN PAGE CONTENT-->
			<div class="row-fluid">
				<div class="span12">
					<!-- BEGIN EXAMPLE TABLE PORTLET-->
					<div class="portlet box blue">
						<div class="portlet-title">
							<h4><i class="icon-edit">Daftar Pengguna</i></h4>
							<!--<div class="tools" >
								<a href="javascript:;" class="reload"></a>
							</div>-->
														
							<div class="actions" style="padding-right: 0px">
							<!--	<a href="" id="addUserBtn" class="btn green"><i class="icon-plus"></i> Tambah Pengguna</a> -->
							</div>
							
						</div>
						<div class="portlet-body">
							<?php //$this->load->view('searchBar', $searchBar);?>
							<table class="table table-striped table-hover table-bordered" id="tblUserList">
								<thead>
									<tr>
										<th>Nama Pengguna</th>
										<th>Nama</th>
										<th>Email Pengguna</th>
										<th>Status Pengguna</th>
										<th>Jabatan</th>
										<th>Profil</th>
										<th>Tanggal Buat</th>
										<th>Nomor Telpon</th>
										<th>Tindakan</th>
									</tr>
								</thead>
								<tbody>
									
									<?php 
										foreach ($users as $user){
											$namaJabatan = "";
											if ($user->getJabatan()!=NULL){
												$namaJabatan = $user->getJabatan()->getNama(); 
											}
											$criteria = array('userId' => $user->getUserId());
											$userProfiles = $this -> em -> getRepository('\models\entities\UserProfile')->findBy($criteria);
											$prof = array();
											foreach ($userProfiles as $row){
												$prof[] = $row->getProfile()->getNamaProfile();
											}
											
											$profiles = "";
											if (is_array($prof)){
												$profiles = implode(", ", $prof);
											}
											$tglBuat = "";
											if ($user->getCreateDate() != NULL) 
											   $tglBuat = $user->getCreateDate()->format('d-m-Y');
											if($user->getStatus() == 1)
											{
												$statusUser = "active";
											}
											else
											{
											    $statusUser = "inactive";
											}
											if($user->getMsisdn() == null)
											{
												$msisdn = "";
											}
											else
											{
											    $msisdn = $user->getMsisdn();
											}
											echo '<tr>
													<td>'.$user->getUserId().'</td>
													<td>'.$user->getName().'</td>
													<td>'.$user->getMail().'</td>
													<td>'.$statusUser.'</td>
													<td>'.$namaJabatan.'</td>
													<td>'.$profiles.'</td>
													<td>'.$tglBuat.'</td>
													<td>'.$msisdn.'</td>
													<td>
														<a class="btn blue mini" href="'.base_url().'administration/userList/'.$user->getUserId().'">Edit</a>&nbsp;
														<!-- <a class="btn red mini" href="">Hapus</a> -->
													</td>
												</tr>';		
										}
									
									?>
																	
								</tbody>
							</table>
							<div class="row-fluid">
								<?php //$this->load->view('paging', $pBerkas);?>
							</div>
						</div>
					</div>
					<!-- END EXAMPLE TABLE PORTLET-->
				</div>
			</div>
			<!-- END PAGE CONTENT -->
		</div>
		<!-- END PAGE CONTAINER-->
	</div>
	<!-- END PAGE -->
</div>
<!-- END CONTAINER -->

<!-- DIALOG -->
<!--
<div id="addUserModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
		<h3 id="myModalLabel1">Tambah Pengguna</h3>		
	</div>
<div class="modal-body">
	<div class="portlet-body fuelux">
		<form action="#" class="form-horizontal">
           <div class="control-group">
              <label class="control-label">Nama Pengguna</label>
              <div class="controls">
                  <input type="text" class="span3" />                  
              </div>
           </div>
           <div class="control-group">
                <label class="control-label">Jabatan</label>
                              <div class="controls">
                                 <select class="span3 chosen" data-placeholder="--Pilih Jabatan--" tabindex="1">
                                    <option value=""></option>
                                    <option value="Category 1">CBO</option>
                                    <option value="Category 2">CF</option>
                                    <option value="Category 3">DQA</option>
                                    <option value="Category 4">Koordinator IT</option>
                 				</select>
              </div>
           </div>
           <div class="control-group">
                <label class="control-label">Jabatan</label>
                <div class="controls">
                   <select data-placeholder="Jabatan" class="chosen span3" tabindex="-1" id="selS0V">
                      <option value=""></option>
                      <optgroup label="Profil Inbox">
                      	<option>Dallas Cowboys</option>
                      	<option>New York Giants</option>
                      	<option>Philadelphia Eagles</option>
                      	<option>Washington Redskins</option>
                      </optgroup>
                      <optgroup label="Profil Administrasi Berkas">
                      	<option>Chicago Bears</option>
                        <option>Detroit Lions</option>
                        <option>Green Bay Packers</option>
                        <option>Minnesota Vikings</option>
                      </optgroup>
					</select>
                </div>
           </div>
       </form>                 
	</div>	
</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
		<button class="btn yellow" id="saveNote">Save</button>
	</div>
</div>-->
<!-- END DIALOG -->
<!--
<div id="cancelModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
		<h3 id="myModalLabel3">Confirm Header</h3>
	</div>
	<div class="modal-body">
		<p>Apakah anda yakin membatalkan laporan ini ?</p>				
	</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true">Tidak</button>
		<button data-dismiss="modal" class="btn blue" id="confirmCancelBtn">Ya</button>
	</div>
</div>
-->	
<div id="alertModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
		<h3 id="myModalLabel2">Informasi</h3>
	</div>
	<div class="modal-body" id="alertMessage">
		
	</div>
	<div class="modal-footer">
		<button data-dismiss="modal" class="btn green">OK</button>
	</div>
</div>

<?php $this->load->view('footer')?>

<script>
	$(document).ready(function() {
		// table grid
        $('#tblUserList').dataTable({
            "aLengthMenu": [
                [10, 25, 50, -1],
                [10, 50, 100, "All"]
            ],
            // set the initial value
            "iDisplayLength": 10,
            "sPaginationType": "bootstrap",
            "oLanguage": {
                "sLengthMenu": "_MENU_ users per page",
                "oPaginate": {
                    "sPrevious": "Prev",
                    "sNext": "Next"
                }
            },
            "aoColumnDefs": [{
                'bSortable': false,
                'aTargets': [0]
            }]
        });
		
		$("#alertModal").draggable({
      		handle: ".modal-header"
  		});
		
		//$('#alertModal').modal('show');
		<?php
			$flash_message = $this->session->userdata('flash_message');
			if ($flash_message != ""){
				echo "$('#alertMessage').html('<p>".$flash_message."</p>');
					  $('#alertModal').modal('show');
					 ";
				$this->session->set_userdata('flash_message',"");
			} 
		?>
		
		$("#addUserModal").draggable({
		      handle: ".modal-header"
		}); 
		 
		$("#addUserModal" ).dialog({
		      height: 300,
		      width: 400,
		      modal: true
		 }); 
		$('#cancelBtn').click(function (event){			
			event.preventDefault();
			$('#pUserId').val("");
			$('#pPassword').val("");
			$('#pJabatan').val("-1");
			$('#pJabatan').trigger("liszt:updated");
			//$("#pProfil").multiselect("clearSelection");
			//$("#pProfil").multiselect( 'refresh' );
			//$("#pProfil > option").attr("selected",false);
			$("#pProfil option:selected").removeAttr("selected");
			$('#pProfil').trigger("liszt:updated");	
			window.location="<?php echo base_url().'administration/userList';?>"		
		});
		
	/*	$('#pUserId').keypress(function (e) {
		    var regex = new RegExp("^[a-zA-Z0-9]+$");
		    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
		    if (regex.test(str)) {
		        return true;
		    }
		    e.preventDefault();
		    return false;
	}); */
		
		$('#saveBtn').click(function(event){
			/*  validasi */
			event.preventDefault();
			/*if ($('#distrik').val() == "-1"){
				alert('District cannot be null...');
				return;
			}*/
			/*
			if ($('#pUserId').val() == ""){
				alert('Silahkan memasukkan nama pengguna');
				return;
			}
			
			if ($('#pUserId').val() == ""){
				alert('Silahkan memasukkan nama pengguna');
				return;
			}
			

			
			var patternUserID = /^[a-zA-Z0-9_]{2,25}$/i;
			var patternName = /^[a-zA-Z ]+$/;
			var patternMail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
            var textUserID = document.getElementById("pUserId");
            var textName = document.getElementById("pName");
            var textMail = document.getElementById("pMail");
            var textActive = document.getElementById("pStatusActive");
            var textInactive = document.getElementById("pStatusInactive");
            var textPassword = document.getElementById("pPassword");
            var textJabatan = document.getElementById("pJabatan");
            var textProfil = document.getElementById("pProfil");
            if(!patternUserID.test(textUserID.value))
            {
            	alert("The username can only consist of alphabetical, number and underscore.\nMinimum 6 characters and maximum 25.");
                return;
            }
			else if(!patternName.test(textName.value))
            {
                alert("Name can only consist of alphabetical and space.\nMinimum 2 characters and maximum 50.");
                return;
            }
			else if(!patternMail.test(textMail.value))
            {
                alert("Please input valid email.");
                return;
            }
			else if(!textActive.checked && !textInactive.checked){
				alert('Please set status of user.');
				return;
			}
			else if(textJabatan.value == -1)
			{
				alert('Please set jabatan of user.');
				return;
			}
			else if(textProfil.value == "")
			{
				alert('Please set profile of user.');
				return;
			}
			else
			{
				
			}
			*/
	  		$('#userForm').submit();
			
	  	});
	});
</script>